package com.summer.filesync.security;

import java.time.LocalDateTime;

/** API密钥实体类 表示系统中的API访问密钥 */
public class ApiKey {
  private final String key;
  private final String owner;
  private final UserRole role;
  private final String description;
  private final boolean enabled;
  private final LocalDateTime createdAt;
  private LocalDateTime lastUsedTime;
  private int usageCount;

  public ApiKey(String key, String owner, UserRole role, String description, boolean enabled) {
    this.key = key;
    this.owner = owner;
    this.role = role;
    this.description = description;
    this.enabled = enabled;
    this.createdAt = LocalDateTime.now();
    this.lastUsedTime = null;
    this.usageCount = 0;
  }

  public String getKey() {
    return key;
  }

  public String getOwner() {
    return owner;
  }

  public UserRole getRole() {
    return role;
  }

  public String getDescription() {
    return description;
  }

  public boolean isEnabled() {
    return enabled;
  }

  public LocalDateTime getCreatedAt() {
    return createdAt;
  }

  public LocalDateTime getLastUsedTime() {
    return lastUsedTime;
  }

  public int getUsageCount() {
    return usageCount;
  }

  public void updateLastUsed() {
    this.lastUsedTime = LocalDateTime.now();
    this.usageCount++;
  }

  @Override
  public String toString() {
    return String.format(
        "ApiKey{owner='%s', role=%s, enabled=%s, usageCount=%d, createdAt=%s}",
        owner, role, enabled, usageCount, createdAt);
  }
}
